// pages/doctor/patientDetail/patientDetail.js
Page({
  data: {
    patientId: null,
    patient: null,
    healthMetrics: null,
    dietHistory: [],
    activeTab: 'overview',
    
    tabs: [
      { key: 'overview', name: '概览' },
      { key: 'health', name: '健康档案' },
      { key: 'diet', name: '饮食记录' },
      { key: 'messages', name: '咨询记录' }
    ],
    
    loading: true
  },

  onLoad(options) {
    if (options.id) {
      this.setData({ patientId: parseInt(options.id) });
      this.loadPatientData();
    }
  },

  // 加载患者数据
  async loadPatientData() {
    this.setData({ loading: true });
    
    try {
      // 模拟数据加载
      const patient = {
        id: this.data.patientId,
        name: '张小明',
        age: 28,
        gender: '男',
        avatar: '/assets/images/avatar1.jpg',
        phone: '138****8888',
        email: 'zhang@example.com',
        registerDate: '2023-06-15',
        lastVisit: '2024-01-15',
        healthStatus: '良好',
        healthMetrics: {
          bmi: 22.5,
          bmiStatus: '正常',
          bodyFatRate: 18.5,
          bloodPressure: { systolic: 120, diastolic: 80 },
          bloodSugar: { fasting: 5.8, postprandial: 7.2 },
          heartRate: 72,
          cholesterol: { total: 4.2, hdl: 1.2, ldl: 2.5 },
          triglycerides: 1.1
        },
        dietHistory: [
          {
            date: '2024-01-15',
            meals: [
              { type: '早餐', time: '08:00', foods: ['燕麦粥', '鸡蛋', '牛奶'], calories: 350 },
              { type: '午餐', time: '12:30', foods: ['米饭', '青菜', '鱼肉'], calories: 480 },
              { type: '晚餐', time: '18:00', foods: ['面条', '蔬菜汤'], calories: 320 }
            ],
            totalCalories: 1150
          },
          {
            date: '2024-01-14',
            meals: [
              { type: '早餐', time: '08:15', foods: ['包子', '豆浆'], calories: 280 },
              { type: '午餐', time: '12:00', foods: ['米饭', '红烧肉', '青菜'], calories: 520 },
              { type: '晚餐', time: '18:30', foods: ['粥', '咸菜'], calories: 200 }
            ],
            totalCalories: 1000
          }
        ],
        messages: [
          {
            id: 1,
            type: 'text',
            content: '医生，我最近血糖有点高，应该怎么调整饮食？',
            sender: 'patient',
            time: '2024-01-15 10:30',
            read: true
          },
          {
            id: 2,
            type: 'text',
            content: '建议您减少精制碳水化合物的摄入，增加蔬菜和蛋白质的比例。',
            sender: 'doctor',
            time: '2024-01-15 10:35',
            read: true
          }
        ]
      };

      this.setData({
        patient,
        healthMetrics: patient.healthMetrics,
        dietHistory: patient.dietHistory,
        loading: false
      });

    } catch (error) {
      console.error('加载患者数据失败:', error);
      this.setData({ loading: false });
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    }
  },

  // 切换标签页
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ activeTab: tab });
  },

  // 开始聊天
  startChat() {
    wx.navigateTo({
      url: `/pages/doctor/chat/chat?patientId=${this.data.patientId}`
    });
  },

  // 编辑患者信息
  editPatient() {
    wx.navigateTo({
      url: `/pages/doctor/editPatient/editPatient?id=${this.data.patientId}`
    });
  },

  // 查看健康报告
  viewHealthReport() {
    wx.navigateTo({
      url: `/pages/doctor/healthReport/healthReport?patientId=${this.data.patientId}`
    });
  },

  // 添加饮食记录
  addDietRecord() {
    wx.navigateTo({
      url: `/pages/doctor/addDietRecord/addDietRecord?patientId=${this.data.patientId}`
    });
  },

  // 导出数据
  exportData() {
    wx.showActionSheet({
      itemList: ['导出健康档案', '导出饮食记录', '导出咨询记录'],
      success: (res) => {
        const actions = ['health', 'diet', 'messages'];
        const action = actions[res.tapIndex];
        this.performExport(action);
      }
    });
  },

  // 执行导出
  performExport(type) {
    wx.showLoading({ title: '导出中...' });
    
    // 模拟导出过程
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: '导出成功',
        icon: 'success'
      });
    }, 2000);
  },

  // 拨打电话
  callPatient() {
    wx.makePhoneCall({
      phoneNumber: this.data.patient.phone.replace(/\*/g, ''),
      fail: () => {
        wx.showToast({
          title: '拨号失败',
          icon: 'none'
        });
      }
    });
  },

  // 发送邮件
  sendEmail() {
    wx.showToast({
      title: '邮件功能开发中',
      icon: 'none'
    });
  }
})
